package cn.wolfcode.p2p.base.service.impl;

import cn.wolfcode.p2p.base.domain.Account;
import cn.wolfcode.p2p.base.domain.LoginInfo;
import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.excetion.DisplayableExcetion;
import cn.wolfcode.p2p.base.mapper.UserInfoMapper;
import cn.wolfcode.p2p.base.service.IAccountService;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.base.util.BitStateUtil;
import cn.wolfcode.p2p.base.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class UserInfoServiceImpl implements IUserInfoService {
    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public int insert(UserInfo record) {
        return userInfoMapper.insert(record);
    }

    @Override
    public int updateByPrimaryKey(UserInfo record) {
        int i = userInfoMapper.updateByPrimaryKey(record);
        if (i == 0) {
            throw new DisplayableExcetion("操作失败,请重新提交");
        }
        return i;
    }

    @Override
    public UserInfo getCurrent() {
        return userInfoMapper.selectByPrimaryKey(UserContext.getCount().getId());
    }

    //基础资料的保存
    @Override
    public void basicInfoSave(UserInfo userInfo) {
        //获取当前用户,然后封信操作
        UserInfo current = this.getCurrent();//使用this查询出当前登录用户
        current.setEducationBackground(userInfo.getEducationBackground());
        current.setHouseCondition(userInfo.getHouseCondition());
        current.setKidCount(userInfo.getKidCount());
        current.setIncomeGrade(userInfo.getIncomeGrade());
        current.setMarriage(userInfo.getMarriage());
        //判官是不是已经填写基本资料验证
        if (!current.isHasBasicInfo()) {
            //更改状态为已经认证
            current.addState(BitStateUtil.HAS_BASIC_INFO);
        }
        this.updateByPrimaryKey(current);
    }

    @Override
    public UserInfo get(Long id) {
        return userInfoMapper.get(id);
    }

}

